package cn.yoyo;

import cn.yoyo.dao.BlogTypeDao;
import cn.yoyo.dao.UserDao;
import cn.yoyo.entity.BlogType;
import cn.yoyo.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

@SpringBootApplication
public class YoyoblogApplication {

    public static void main(String[] args) {
        ApplicationContext ioc =  SpringApplication.run(YoyoblogApplication.class, args);
        PasswordEncoder passwordEncoder = ioc.getBean(PasswordEncoder.class);
        UserDao userDao = ioc.getBean(UserDao.class);
        List<User> userList = userDao.findAll();
        if (userList.isEmpty()){
            User adm = new User();
            adm.setNickname("管理员");
            adm.setUsername("adm");
            adm.setPassword(passwordEncoder.encode("123456"));
            adm.setRole("A");

            User cons = new User();
            cons.setNickname("普通用户");
            cons.setUsername("cons");
            cons.setPassword(passwordEncoder.encode("123456"));
            cons.setRole("C");

            userDao.add(adm);
            userDao.add(cons);
        }

        BlogTypeDao blogTypeDao = ioc.getBean(BlogTypeDao.class);
        List<BlogType> typeList = blogTypeDao.findAll();
        if (typeList.isEmpty()){
            BlogType blogType1 = new BlogType();
            blogType1.setName("java后端开发");

            BlogType blogType2 = new BlogType();
            blogType2.setName("Vue前端开发");

            blogTypeDao.add(blogType1);
            blogTypeDao.add(blogType2);
        }
    }

}
